package com.sdp.wz.handleImpl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.wz.db.wz_input.entity.WzInput;
import com.sdp.wz.db.wz_input.service.IWzInputService;
import com.sdp.wz.db.wz_input_detail.entity.WzInputDetail;
import com.sdp.wz.db.wz_input_detail.service.IWzInputDetailService;
import com.sdp.wz.db.wz_now_num.service.IWzNowNumService;
import com.sdp.wz.db.wz_serial_code.service.IWzSerialCodeService;
import com.sdp.wz.db.wz_warehouse_info.entity.WzWarehouseInfo;
import com.sdp.wz.db.wz_warehouse_info.service.IWzWarehouseInfoService;
import com.sdp.wz.handle.WzInOutPutHandle;
import com.sdp.wz.utils.CurrentLoginEmp;
import com.sdp.wz.utils.WzConstants;

/**
 * 物资直接入库
 * 没有采购单，到货单等操作，直接录入入库物资信息进行入库
 * @author zhaoshaopo
 *
 */
@Component
public class WzDirectInputHandleImpl extends WzInOutPutHandle{
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@Autowired
	private IWzWarehouseInfoService iWzWarehouseInfoService;

	
	@Autowired
	private IWzInputService iWzInputService;
	
	@Autowired
	private IWzInputDetailService iWzInputDetailService;
	
	@Autowired
	private IWzSerialCodeService iWzSerialCodeService;
	
	@Autowired
	private IWzNowNumService iWzNowNumService;

	@Override
	public BusinessContext execute(BusinessContext businessContext) {
		return ApplicationContextUtils.getBean(WzDirectInputHandleImpl.class).wzProcuredInput(businessContext);
	}
	
	@Transactional(rollbackFor=Exception.class)
	public BusinessContext wzProcuredInput(BusinessContext businessContext) {
		
		//检测是否登录
		JSONObject emp = currentLoginEmp.getCurrentLoginInfo();
		if(StringUtils.isEmpty(emp)) {
			businessContext.setMessage("请先登录系统");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		List<WzInputDetail> wzList = businessContext.get(WzInputDetail.class);
		WzInput input = businessContext.get(WzInput.class);
		
		//检查入库物资是否存在
		if(input==null || wzList==null || wzList.size()==0) {
			businessContext.setSuccess(false);
			businessContext.setMessage("入库信息为空，不能入库");
			return businessContext;
		}
		
		
		WzWarehouseInfo warehouseInfo = iWzWarehouseInfoService.queryWarehouseInfoByCode(input.getWarehouseCode());
		
		//检查仓库是否停用
		if(warehouseInfo==null || WzConstants.否.getCode().equals(warehouseInfo.getIsUse())) {
			businessContext.setMessage("此仓库已停用，不能入库");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		//检查仓库是否锁定
		if(warehouseInfo==null || WzConstants.是.getCode().equals(warehouseInfo.getIsLock())) {
			businessContext.setMessage("仓库目前处于盘存状态，已锁定，无法进行入库");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		//检测是否有权限
		if(StringUtils.isEmpty(warehouseInfo.getWarehouseManagement())) {
			businessContext.setMessage("此仓库未配置库管人员");
			businessContext.setSuccess(false);
			return businessContext;
		}
		//检测是否有权限
		String[] managements =  warehouseInfo.getWarehouseManagement().split(",");
		boolean contains = Arrays.asList(managements).contains(emp.getString("empNo"));
		if(!contains) {
			businessContext.setMessage("您没有此仓库的入库权限");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		
		LocalDateTime now = LocalDateTime.now();
		if(input.getInputTime()==null) {
			input.setInputTime(now);
		}
		if(input.getInputDate()==null) {
			input.setInputDate(input.getInputTime().toLocalDate());
		}
		
		if(input.getCreateTime()==null) {
			input.setCreateTime(now);
		}
		//生成入库单号
		if(StringUtils.isEmpty(input.getInputCode())) {
			String serialCode = iWzSerialCodeService.createSerialCode(WzConstants.流水号前缀_物资入库单.getCode());
			if(StringUtils.isEmpty(serialCode)) {
				businessContext.setMessage("入库单号生成失败，请稍后再试");
				businessContext.setSuccess(false);
				return businessContext;
			}
			input.setInputCode(serialCode);
		}
		

		input.setOpName(emp.getString("empName"));
		input.setOpNo(emp.getString("empNo"));
		input.setIsUse(WzConstants.是.getCode());
		input.setIsOutput(WzConstants.否.getCode());//此入库单是否已经出库
		
		Map<String,BigDecimal> wzInputNumMap = new HashMap<>();
		List<String> wzNoList = new ArrayList<String>();
		
		for(WzInputDetail wz : wzList) {
			wz.setInputCode(input.getInputCode());
			wz.setNowNum(wz.getInputNum());
			wz.setCreateTime(now);
			wz.setOrgNo(input.getOrgNo());
			wzNoList.add(wz.getWzNo());
			if(wzInputNumMap.containsKey(wz.getWzNo())) {
				wzInputNumMap.put(wz.getWzNo(), wz.getInputNum().add(wzInputNumMap.get(wz.getWzNo())));
			}else {
				wzInputNumMap.put(wz.getWzNo(), wz.getInputNum());
			}
		}
		
		//入库操作
		iWzInputService.saveOrUpdate(input);
		iWzInputDetailService.saveOrUpdateBatch(wzList);
		
		//计算库存
		iWzNowNumService.wzNowNumAdd(wzNoList, wzInputNumMap, warehouseInfo);
		
		
		return businessContext;
	}

}
